﻿#if UNITY_EDITOR
using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;
/// <summary>
/// XML资源
/// </summary>
public class ESF_BuildXmlAsset<X> : AbsESF_EditorBuildAsset
    where X : AbsESF_Xml, new()
{
    /// <summary>
    /// Xml类
    /// </summary>
    public X xml { get; private set; }
    /// <summary>
    /// 滚动视图位置
    /// </summary>
    Vector2 mScrollViewPosition = Vector2.zero;
    /// <summary>
    /// 是否显示绘制对象的ScrollView
    /// </summary>
    bool mShowDrawObjectScrollView = false;

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_directoryRelativeCallerFilePath">相对于调用文件目录</param>
    /// <param name="_sourceFilePath">源文件路径</param>
    public ESF_BuildXmlAsset(string _fileName, string _directoryRelativeCallerFilePath = "", [CallerFilePath] string _sourceFilePath = "")
        : this(_fileName, ESF_Utility.SubToFilePath(_directoryRelativeCallerFilePath, _sourceFilePath))
    {

    }

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="_fileName">文件名称</param>
    /// <param name="_directory">文件目录</param>
    public ESF_BuildXmlAsset(string _fileName, string _directory)
        : base(_fileName, _directory, enESF_UnityFileExt.Xml)
    {
        drawSelectFilterEnums = OnDrawSelectFilterEnums();        
        LoadAsset();
    }

    /// <summary>
    /// 搜索项折叠
    /// </summary>
    bool mSearchEnumFoldout = false;
    /// <summary>
    /// 绘制操作类型
    /// </summary>
    ESF_enGUIOperationClassify mDrawOperationClassify = ESF_enGUIOperationClassify.All;
   
    /// <summary>
    /// 绘制GUI
    /// </summary>
    protected override void OnDrawGUI()
    {
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField($"{OnGetFileDesc()}：{filePath}");
        if (GUILayout.Button(ESF_enGUIDisplayWord.Brower.ESF_ToAlias()))
        {
            ESF_Utility.PingObject(filePath);
        }
        EditorGUILayout.EndHorizontal();
        ESF_Utility.DrawSeparator();
        
        OnDrawLayoutSetting();
        OnDrawEnumFilterLayout();
        if (xml != null)
        {
            bool foldout = false;
            if (mShowDrawObjectScrollView)
            {
                mScrollViewPosition = EditorGUILayout.BeginScrollView(mScrollViewPosition);
            }            
            OnBeforeDrawGUI();
            foldout = ESF_Utility.DrawObject(xml, drawSelectFilterEnums, mDrawOperationClassify, OnAllowDrawField, OnAllowOperationArraySubItem);
            OnAfterDrawGUI();
            if (mShowDrawObjectScrollView)
            {
                EditorGUILayout.EndScrollView();
            }
            mShowDrawObjectScrollView = foldout;
            ESF_Utility.DrawSeparator();
        }
    }

    /// <summary>
    /// 获得文件描述
    /// </summary>
    /// <returns>文件描述</returns>
    protected virtual string OnGetFileDesc() { return typeof(X).Name; }

    #region OnDrawLayoutSetting 绘制布局设定
    /// <summary>
    /// 绘制布局设定
    /// </summary>
    private void OnDrawLayoutSetting()
    {
        mDrawOperationClassify = (ESF_enGUIOperationClassify)ESF_Utility.EnumPopup(mDrawOperationClassify, $"{ESF_enGUIDisplayWord.Sift.ESF_ToAlias()}：{typeof(ESF_enGUIOperationClassify).ESF_ToAlias()}");
    }
    #endregion

    #region drawSelectFilterEnums 绘制选择枚举过滤布局
    /// <summary>
    /// 绘制选择枚举过滤布局
    /// </summary>
    /// <returns>过滤布局</returns>
    protected virtual Type[] OnDrawSelectFilterEnums() { return null; }
    /// <summary>
    /// 绘制选择枚举过滤布局
    /// </summary>
    Type[] drawSelectFilterEnums = null;
    /// <summary>
    /// 绘制枚举过滤布局
    /// </summary>
    private void OnDrawEnumFilterLayout()
    {
        if (drawSelectFilterEnums != null)
        {
            mSearchEnumFoldout = EditorGUILayout.BeginFoldoutHeaderGroup(mSearchEnumFoldout, ESF_enGUIDisplayWord.Search.ESF_ToAlias());
            if (mSearchEnumFoldout)
            {
                for (int i = 0; i < drawSelectFilterEnums.Length; i++)
                {
                    ESF_Utility.DrawEnumSearch(drawSelectFilterEnums[i]);
                    if (i != drawSelectFilterEnums.Length - 1)
                    {
                        ESF_Utility.DrawSeparator();
                    }
                }
            }
            EditorGUILayout.EndFoldoutHeaderGroup();
            ESF_Utility.DrawSeparator();
        }
    }
    #endregion

    #region OnAllowOperationArraySubItem 是否允许数组子项操作    
    /// <summary>
    /// 是否允许数组子项操作
    /// </summary>
    /// <param name="_field">字段</param>
    /// <param name="_fieldObject">字段所属对象</param>
    /// <param name="_fieldDesc">字段描述</param>
    /// <param name="_isArray">是否数组</param>
    /// <param name="_isValueType">是否是值类型</param>
    /// <param name="_index">数组索引</param>
    /// <param name="_oprClassify">操作类别</param>
    /// <param name="_drawClassify">绘制类别</param>
    /// <returns>true:允许,false:不允许</returns>
    protected virtual bool OnAllowOperationArraySubItem(FieldInfo _field, object _fieldObject, string _fieldDesc, bool _isArray, bool _isValueType, int _index, ESF_enGUIOperationClassify _oprClassify, ESF_enGUIDrawClassify _drawClassify) { return true; }
    #endregion

    #region OnAllowDrawField 是否允许绘制字段
    /// <summary>
    /// 是否允许绘制字段
    /// </summary>
    /// <param name="_field">字段</param>
    /// <param name="_fieldObject">字段所属对象</param>
    /// <param name="_fieldDesc">字段描述</param>
    /// <param name="_isArray">是否数组</param>
    /// <param name="_isValueType">是否是值类型</param>
    /// <param name="_index">数组索引</param>
    /// <param name="_oprClassify">操作类别</param>
    /// <param name="_drawClassify">绘制类别</param>
    /// <returns>true:允许,false:不允许</returns>
    protected virtual bool OnAllowDrawField(FieldInfo _field, object _fieldObject, string _fieldDesc, bool _isArray, bool _isValueType, int _index, ESF_enGUIOperationClassify _oprClassify, ESF_enGUIDrawClassify _drawClassify) { return true; }
    #endregion

    /// <summary>
    /// Before绘制GUI
    /// </summary>
    protected virtual void OnBeforeDrawGUI() { }

    /// <summary>
    /// After绘制GUI
    /// </summary>
    protected virtual void OnAfterDrawGUI() { }

    /// <summary>
    /// 创建资源
    /// </summary>
    protected override void OnCreateAsset()
    {
        if (xml == null)
        {
            xml = new X();
            xml.Resolve();
        }
        File.WriteAllText(filePath, xml.ESF_XmlSerialize());
    }

    /// <summary>
    /// 保存资源
    /// </summary>
    /// <returns>true:保存成功,false:保存失败</returns>
    protected override bool OnSaveAsset()
    {
        bool result = true;
        if (exists)
        {
            result = ESF_Utility.CheckSave(xml);
            if (result)
            {
                xml.Resolve();
                File.WriteAllText(filePath, xml.ESF_XmlSerialize());
            }
        }
        else
        {
            CreateAsset();
        }
        return result;
    }

    /// <summary>
    /// 加载资源
    /// </summary>
    protected override void OnLoadAsset()
    {
        if (exists)
        {            
            xml = File.ReadAllText(filePath).ESF_XmlDeserialize<X>();
            if (xml != null)
            {
                xml.Resolve();
            }
            else
            {
                ESF_P.ESF_Error($"Can't XmlDeserialize to {typeof(X)} by file =>{filePath}");
            }
        }
        else
        {
            CreateAsset();
        }
    }
}
#endif